CellPrint
Creates a new cell with a given content in a notebook
CellPrint[str_String, opts___] _RemoteCellObj
where str
is a text representation of an expression to be inserted into a new cell.
Options
By the default it prints an output Wolfram Language cell after a current parent input cell, but you can override it
"After"
Specifies a RemoteCellObj after which a new cell will be inserted. A sequence patterns are also supported
"Type"
The default value is "Output"
"Target"
Default is "Notebook"
. Specifies the target, where a cell will be printed. Possible values
"Notebook"
orAutomatic
orNull
"Window"
or_
projects to a new window
"Display"
Default is "codemirror"
. This sets what renderer will be used, the following renders are available
"codemirror"
default render for all input cells and output WL cells"markdown"
"wlx"
- `"html"
- `"js"
and etc depending on the cell types available on your system (see Cell types)
"EvaluatedQ"
Sets if the evaluation required. By the default is True
(no evaluation, display as provided)
"Notebook"
In a case if the evaluation context (if After
is not provided and it is called by a button other async event) is not available, you should provide a reference to the notebook (see EvaluationNotebook)
Examples
Basic
Print a cell after the current one with a date
With[{cell = ResultCell[]},
EventHandler[InputButton[], Function[Null,
CellPrint[ToString[Now, StandardForm], "After"->cell]
]]
]
or print to a new window
cell = CellPrint[ToString[Plot[x,{x,0,1}], StandardForm], "Target"->_];
and then delete it
Delete[cell];
Different renders
One can use WLX as well. For example you create a template in a separate cell
.wlx
wlTest[P_] := <div class="p-1 rounded" style="border: 1px solid blue"><P/></div>
Since this function returns a string, we can pass it directly to CellPrint
CellPrint[wlTest[Plot[x, {x,0,1}]], "Display"->"wlx"];
A window, that opens another window
It is still possible to do if you provide notebook in Options, otherwise it does not know where to open it
man := ManipulatePlot[Sin[x w], {x,-1,1}, {w,1,5}];
open[n_] := CellPrint[ToString[man, StandardForm], "Target"->_, "Notebook"->n];
CellPrint[ToString[
With[{n = EvaluationNotebook[]},
EventHandler[InputButton[], Function[Null, open[n]]]
]
, StandardForm], "Target"->_]
Even if you close your notebook, it will still work.